// dllmain.cpp : Defines the entry point for the DLL application.
#define VPX_CODEC_DISABLE_COMPAT 1

#include "stdafx.h"
#include <Windows.h>
#include "vpx_image.h"
#include "vpx_codec.h"
#include "vp8.h"
#include "inttypes.h"
#include "stdint.h"
#include "vpx_codec.h"
#include "vp8.h"
#include "vpx_decoder.h"
#include "vpx_encoder.h"

#include "vp8dx.h"
#define interface (vpx_codec_vp8_dx())

const char*(*_vpx_codec_build_config)(void);
vpx_codec_err_t(*_vpx_codec_control_)(vpx_codec_ctx_t  *ctx, int ctrl_id,...);
vpx_codec_err_t(*_vpx_codec_dec_init_ver)(vpx_codec_ctx_t*ctx,vpx_codec_iface_t*iface,vpx_codec_dec_cfg_t*cfg,vpx_codec_flags_t flags,int ver);
vpx_codec_err_t(*_vpx_codec_decode)(vpx_codec_ctx_t*ctx,const uint8_t*data,unsigned int data_sz,void*user_priv,long deadline);
vpx_codec_err_t(*_vpx_codec_destroy)(vpx_codec_ctx_t *ctx);
vpx_codec_err_t(*_vpx_codec_enc_config_default)(vpx_codec_iface_t*iface,vpx_codec_enc_cfg_t*cfg,unsigned int usage);
vpx_codec_err_t(*_vpx_codec_enc_config_set)(vpx_codec_ctx_t*ctx,const vpx_codec_enc_cfg_t*cfg);
vpx_codec_err_t(*_vpx_codec_enc_init_ver)(vpx_codec_ctx_t*ctx,vpx_codec_iface_t*iface,vpx_codec_enc_cfg_t*cfg,vpx_codec_flags_t flags,int ver);
vpx_codec_err_t(*_vpx_codec_encode)(vpx_codec_ctx_t*ctx,const vpx_image_t*img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t flags,unsigned long deadline);
const char*(*_vpx_codec_err_to_string)(vpx_codec_err_t  err);
const char*(*_vpx_codec_error)(vpx_codec_ctx_t*ctx);
const char*(*_vpx_codec_error_detail)(vpx_codec_ctx_t*ctx);
vpx_codec_caps_t(*_vpx_codec_get_caps)(vpx_codec_iface_t *iface);
const vpx_codec_cx_pkt_t*(*_vpx_codec_get_cx_data)(vpx_codec_ctx_t*ctx,vpx_codec_iter_t*iter);
vpx_image_t*(*_vpx_codec_get_frame)(vpx_codec_ctx_t  *ctx,vpx_codec_iter_t *iter);
vpx_fixed_buf_t*(*_vpx_codec_get_global_headers)(vpx_codec_ctx_t*ctx);
vpx_codec_err_t(*_vpx_codec_get_mem_map)(vpx_codec_ctx_t*ctx,vpx_codec_mmap_t*mmap,vpx_codec_iter_t*iter);
const vpx_image_t*(*_vpx_codec_get_preview_frame)(vpx_codec_ctx_t*ctx);
vpx_codec_err_t(*_vpx_codec_get_stream_info)(vpx_codec_ctx_t*ctx,vpx_codec_stream_info_t*si);
const char*(*_vpx_codec_iface_name)(vpx_codec_iface_t *iface);
vpx_codec_err_t(*_vpx_codec_peek_stream_info)(vpx_codec_iface_t*iface,const uint8_t*data,unsigned int data_sz,vpx_codec_stream_info_t*si);
vpx_codec_err_t(*_vpx_codec_register_put_frame_cb)(vpx_codec_ctx_t*ctx,vpx_codec_put_frame_cb_fn_t cb,void*user_priv);
vpx_codec_err_t(*_vpx_codec_register_put_slice_cb)(vpx_codec_ctx_t*ctx,vpx_codec_put_slice_cb_fn_t cb,void*user_priv);
vpx_codec_err_t(*_vpx_codec_set_cx_data_buf)(vpx_codec_ctx_t*ctx,const vpx_fixed_buf_t*buf,unsigned int pad_before,unsigned int pad_after);
vpx_codec_err_t(*_vpx_codec_set_mem_map)(vpx_codec_ctx_t*ctx,vpx_codec_mmap_t*mmaps,unsigned int num_maps);
int(*_vpx_codec_version)(void);
const char*(*_vpx_codec_version_extra_str)(void);
const char*(*_vpx_codec_version_str)(void);
vpx_codec_iface_t* (*_vpx_codec_vp8_cx)(void);
vpx_codec_iface_t* (*_vpx_codec_vp8_dx)(void);
vpx_image_t*(*_vpx_img_alloc)(vpx_image_t  *img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align);
void(*_vpx_img_flip)(vpx_image_t*img);
void(*_vpx_img_free)(vpx_image_t*img);
int(*_vpx_img_set_rect)(vpx_image_t*img,unsigned int x,unsigned int y,unsigned int w,unsigned int h);
vpx_image_t*(*_vpx_img_wrap)(vpx_image_t*img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align,unsigned char*img_data);

void loadLib(const wchar_t*libpath){
	HMODULE handle=LoadLibrary(libpath);
	_vpx_codec_build_config=(const char *(*)(void))GetProcAddress(handle,"vpx_codec_build_config");
	_vpx_codec_control_=(vpx_codec_err_t (*)(vpx_codec_ctx_t  *ctx, int ctrl_id,...))GetProcAddress(handle,"vpx_codec_control_");
	_vpx_codec_dec_init_ver=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_iface_t*iface,vpx_codec_dec_cfg_t*cfg,vpx_codec_flags_t flags,int ver)) GetProcAddress(handle,"vpx_codec_dec_init_ver");
	_vpx_codec_decode=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,const uint8_t*data,unsigned int data_sz,void*user_priv,long deadline))GetProcAddress(handle,"vpx_codec_decode");
	_vpx_codec_destroy=(vpx_codec_err_t (*)(vpx_codec_ctx_t *ctx))GetProcAddress(handle,"vpx_codec_destroy");
	_vpx_codec_enc_config_default=(vpx_codec_err_t  (*)(vpx_codec_iface_t*iface,vpx_codec_enc_cfg_t*cfg,unsigned int usage))GetProcAddress(handle,"vpx_codec_enc_config_default");
	_vpx_codec_enc_config_set=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,const vpx_codec_enc_cfg_t*cfg))GetProcAddress(handle,"vpx_codec_enc_config_set");
	_vpx_codec_enc_init_ver=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_iface_t*iface,vpx_codec_enc_cfg_t*cfg,vpx_codec_flags_t flags,int ver))GetProcAddress(handle,"vpx_codec_enc_init_ver");
	_vpx_codec_encode=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,const vpx_image_t*img,vpx_codec_pts_t pts,unsigned long duration,vpx_enc_frame_flags_t flags,unsigned long deadline))GetProcAddress(handle,"vpx_codec_encode");
	_vpx_codec_err_to_string=(const char* (*)(vpx_codec_err_t  err))GetProcAddress(handle,"vpx_codec_err_to_string");
	_vpx_codec_error=(const char* (*)(vpx_codec_ctx_t  *ctx))GetProcAddress(handle,"vpx_codec_error");
	_vpx_codec_error_detail=(const char* (*)(vpx_codec_ctx_t*ctx))GetProcAddress(handle,"vpx_codec_error_detail");
	_vpx_codec_get_caps=(vpx_codec_caps_t (*)(vpx_codec_iface_t *iface))GetProcAddress(handle,"vpx_codec_get_caps");
	_vpx_codec_get_cx_data=(const vpx_codec_cx_pkt_t*(*)(vpx_codec_ctx_t*ctx,vpx_codec_iter_t*iter))GetProcAddress(handle,"vpx_codec_get_cx_data");
	_vpx_codec_get_frame=(vpx_image_t*(*)(vpx_codec_ctx_t  *ctx,vpx_codec_iter_t *iter))GetProcAddress(handle,"vpx_codec_get_frame");
	_vpx_codec_get_global_headers=(vpx_fixed_buf_t*(*)(vpx_codec_ctx_t*ctx))GetProcAddress(handle,"vpx_codec_get_global_headers");
	_vpx_codec_get_mem_map=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_mmap_t*mmap,vpx_codec_iter_t*iter))GetProcAddress(handle,"vpx_codec_get_mem_map");
	_vpx_codec_get_preview_frame=(const vpx_image_t*(*)(vpx_codec_ctx_t*ctx))GetProcAddress(handle,"vpx_codec_get_preview_frame");
	_vpx_codec_get_stream_info=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_stream_info_t*si))GetProcAddress(handle,"vpx_codec_get_stream_info");
	_vpx_codec_iface_name=(const char*(*)(vpx_codec_iface_t *iface))GetProcAddress(handle,"vpx_codec_iface_name");
	_vpx_codec_peek_stream_info=(vpx_codec_err_t (*)(vpx_codec_iface_t*iface,const uint8_t*data,unsigned int data_sz,vpx_codec_stream_info_t*si))GetProcAddress(handle,"vpx_codec_peek_stream_info");
	_vpx_codec_register_put_frame_cb=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_put_frame_cb_fn_t cb,void*user_priv))GetProcAddress(handle,"vpx_codec_register_put_frame_cb");
	_vpx_codec_register_put_slice_cb=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_put_slice_cb_fn_t cb,void*user_priv))GetProcAddress(handle,"vpx_codec_register_put_slice_cb");
	_vpx_codec_set_cx_data_buf=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,const vpx_fixed_buf_t*buf,unsigned int pad_before,unsigned int pad_after))GetProcAddress(handle,"vpx_codec_set_cx_data_buf");
	_vpx_codec_set_mem_map=(vpx_codec_err_t (*)(vpx_codec_ctx_t*ctx,vpx_codec_mmap_t*mmaps,unsigned int num_maps))GetProcAddress(handle,"vpx_codec_set_mem_map");
	_vpx_codec_version=(int(*)(void))GetProcAddress(handle,"vpx_codec_version");
	_vpx_codec_version_extra_str=(const char*(*)(void))GetProcAddress(handle,"vpx_codec_version_extra_str");
	_vpx_codec_version_str=(const char*(*)(void))GetProcAddress(handle,"vpx_codec_version_str");
	_vpx_codec_vp8_cx=(vpx_codec_iface_t* (*)(void))GetProcAddress(handle,"vpx_codec_vp8_cx");
	GetProcAddress(handle,"vpx_codec_vp8_cx_algo");
	_vpx_codec_vp8_dx=(vpx_codec_iface_t* (*)(void))GetProcAddress(handle,"vpx_codec_vp8_dx");
	GetProcAddress(handle,"vpx_codec_vp8_dx_algo");

	_vpx_img_alloc=(vpx_image_t*(*)(vpx_image_t  *img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align))GetProcAddress(handle,"vpx_img_alloc");
	_vpx_img_flip=(void (*)(vpx_image_t *img))GetProcAddress(handle,"vpx_img_flip");
	_vpx_img_free=(void (*)(vpx_image_t *img))GetProcAddress(handle,"vpx_img_free");
	_vpx_img_set_rect=(int(*)(vpx_image_t*img,unsigned int x,unsigned int y,unsigned int w,unsigned int h))GetProcAddress(handle,"vpx_img_set_rect");
	_vpx_img_wrap=(vpx_image_t*(*)(vpx_image_t*img,vpx_img_fmt_t fmt,unsigned int d_w,unsigned int d_h,unsigned int align,unsigned char*img_data))GetProcAddress(handle,"vpx_img_wrap");
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		//loadLib();
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

